<!--
 * @Author: ZechariahZheng
 * @Date: 2021-12-16 16:07:33
 * @LastEditors: ZechariahZheng
 * @LastEditTime: 2022-04-28 10:32:53
 * @Description: 永中打开文档
-->
<template>
<div>
    <!-- <x-button v-if="(abPermission==='w' || abPermission==='b')" @click.native="openFile" style="width: 30vw">编辑正文</x-button> -->
    <!-- <x-button v-else @click.native="preViewFile" style="width: 30vw">查看正文</x-button> -->
    <x-button @click.native="downloadFile" style="width: 30vw">下载正文</x-button>
</div>
</template>

<script>
import { XButton } from "vux";
import setting from "@/setting";

export default {
    components: {
        XButton
    },
    props: {
        value: {},
        abPermission:{
            type:String,
            default:"r"
        },
    },
    created() {
        var userMsgJson = localStorage.getItem("userMsg");
        var userMsg = JSON.parse(userMsgJson);
        this.userFullName = userMsg.user.fullname;
    },
    data() {
        return {
            userFullName: "",
        }
    },
    methods: {
        getId() {
            let date = Date.now();
            let rund = Math.ceil(Math.random()*1000)
            let id = date+""+rund;
            return id;
        },
        downloadFile() {
            let docFileName = JSON.parse(this.value).docFileName;
            window.open(setting.downLoadFileUrl+docFileName, '_self')
        },
        openFile() {
            let docFileName = JSON.parse(this.value).docFileName;
            let tmpData = {
                method: 1,
                params: {
                    filePath: setting.downLoadFileUrl+docFileName,
                    callbackUrl: setting.upLoadFileUrl,
                    fileId: this.getId(),
                    userId: this.userFullName,
                    extraData: {
                        custom_red_model: "0",
                        TrackRevisions: "1"
                    },
                    fileName: docFileName
                }
            };
            let sendData = {
                jsonParams: JSON.stringify(tmpData)
            }
            this.baseService.postForm(setting.yzEditUrl, sendData).then(result => {
                if (result.errorCode == 0) {
                    // window.open(result.result.urls, '_self');
                    this.$router.push({name:'yzFile', params:{url: result.result.urls}});
                }
            }, function (status) {
                if (status == !0) {
                    this.$message.error('加载失败！' + status);
                }
            });
        },
        preViewFile() {
            let docFileName = JSON.parse(this.value).docFileName;
            let tmpData = {
                method: 1,
                params: {
                    filePath: setting.downLoadFileUrl+docFileName,
                    // callbackUrl: setting.upLoadFileUrl,
                    fileId: "54",
                    userId: this.userFullName,
                    extraData: {
                        custom_red_model: "0",
                        TrackRevisions: "1"
                    },
                    fileName: docFileName
                }
            };
            let sendData = {
                jsonParams: JSON.stringify(tmpData)
            }
            this.baseService.postForm(setting.yzEditUrl, sendData).then(result => {
                if (result.errorCode == 0) {
                    // window.open(result.result.urls, '_self');
                    this.$router.push({name:'yzFile', params:{url: result.result.urls}});
                }
            }, function (status) {
                if (status == !0) {
                    this.$message.error('加载失败！' + status);
                }
            });
        }
    }
}
</script>

<style>

</style>